﻿using LibVLCSharp.Shared;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using WeighClient.Model;

namespace WeighClient.Windows
{
    /// <summary>
    /// PurchaseInfoWindow.xaml 的交互逻辑
    /// </summary>
    public partial class PurchaseInfoWindow : Window
    {
        Bill billModel;
        string labelMsg = "";
        private DispatcherTimer timer;
        private String TextProperty1 = String.Empty;
        public String ImagePath;
/*        LibVLC _libVlc;
        private LibVLC _libVLC;
        private LibVLCSharp.Shared.MediaPlayer _mediaPlayer;
*/


        public PurchaseInfoWindow(Bill bill,string ImagePath1, string ImagePath2)
        {
            InitializeComponent();
            InitializeComponent();
            billModel = bill;
            this.DataContext = billModel;
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += new EventHandler(timeCycle);
            timer.Start();
            if (ImagePath1 != null && ImagePath1 != "")
            {
                myImage1.Source = new BitmapImage(new Uri(ImagePath1));
            }
            if (ImagePath2 != null && ImagePath2 != "")
            {
                myImage2.Source = new BitmapImage(new Uri(ImagePath2));
            }
            /*            InitializeVLC();播放视频
                        PlayRtspStream();*/
        }
        public void timeCycle(object sender, EventArgs e)
        {
            new Thread(() =>
            {
                Application.Current.Dispatcher.BeginInvoke(
                    new Action(() =>
                    {
                        TextProperty1 = DateTime.Now.ToString();
                        this.labelTime.Text = TextProperty1;
                    }));
            }).Start();
        }
        public void SetImagePath(string imagePath)
        {
            this.ImagePath = imagePath;
        }

        public void UpdateBill(Bill bill)
        {
            billModel = bill;
            this.DataContext = billModel;

        }
        public void LabelMsg(string msg)
        {
            this.labelMsg = msg;
            errorLabel.Content = labelMsg;

        }
/*        private void InitializeVLC()
        {
            Core.Initialize();
            _libVLC = new LibVLC();
            _mediaPlayer = new LibVLCSharp.Shared.MediaPlayer(_libVLC);
           // VideoViewControl.MediaPlayer = _mediaPlayer;
        }*/

/*        private void PlayRtspStream()
        {
            // 替换为你的摄像头RTSP地址
            var rtspUrl = "rtsp://admin:admin@192.168.1.41:8554/stream1";

            using (var media = new Media(_libVLC, rtspUrl, FromType.FromLocation))
            {
                // 设置网络缓存（单位：毫秒）
                media.AddOption(":network-caching=200");
                _mediaPlayer.Play(media);
            }
        }*/

        // Window
        // .窗口关闭时释放资源
   /*     protected override void OnClosed(EventArgs e)
        {
            _mediaPlayer?.Stop();
            _mediaPlayer?.Dispose();
            _libVLC?.Dispose();//视频关闭
            //Debug.WriteLine("窗口已完全关闭，资源释放中...");
            base.OnClosed(e);  // 再调用基类方法（重要！否则可能破坏 WPF 窗口生命周期）
        }*/
    }
}
